﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Text;
using MicropleSoft.WmsApp.Library.ClientProxy;
using MicropleSoft.WmsApp.Library.ServiceFacade;


namespace MicropleSoft.WmsApp.Library.Security
{
    public class UserSession
    {
        public static readonly IServiceFacadeChannel ServiceFacadeChannel = ClientProxyFactory.GetServiceFacadeClient();
        public const string AdminUser = "admin";
        public const string PrixToken = "token:";
        public static WmsUser CurrentUser { get; private set; }
        public static string Token { get; private set; }
        public static bool IsLogOn { get; private set; }
        public static Dictionary<String, String> LoginInfo { get; private set; }
        public static string CurrentWhId { get; private set; }

        static UserSession()
        {
            IsLogOn = false;
        }

        public static string Login(string userId, string password,string whId)
        {
            string result = ServiceFacadeChannel.Login(userId, password, whId);

            if (result.StartsWith(PrixToken))
            {
                IsLogOn = true;
                Token = result.Substring(PrixToken.Length);
                CurrentUser = ServiceFacadeChannel.GetSystemUser(userId);
                CurrentWhId = whId;
                LoginInfo = ServiceFacadeChannel.GetLoginInfo(Token);
            }
            return result;
        }
        public static bool CheckVersion()
        {
            string lowestVersion = null;// Factory.WmsServiceFactory.GetServiceClient().GetLowestVersion();
            string localVersion = Assembly.GetExecutingAssembly().GetName().Version.ToString();
            if (String.Compare(localVersion.Substring(0, 1), lowestVersion) < 0)
            {
                throw new Exception("本地程序版本太老，请更新程序后再试试！");
                return false;
            }
            return true;
        }
    }
}
